package com.gxar.quick.ar.server.base.sdk;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.*;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.gxar.common.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 阿里云短信服务sdk
 *
 * @author Linsy
 */
public class AliSmsSDK {

    //产品名称:云通信短信API产品,开发者无需替换
    private static final String PRODUCT = "Dysmsapi";
    //产品域名,开发者无需替换
    private static final String DOMAIN = "dysmsapi.aliyuncs.com";

    private static final String ACCESS_KEY_ID = "LTAImZq4DNH1lhWq";
    private static final String ACCESS_KEY_SECRET = "mSkPiJuEgd0fkYqbh2PZy6V2gvZhlS";

    /**
     * 发送短信
     *
     * @param signName     签名名称
     * @param templateCode 模板code
     * @param phone        手机号
     * @param param        参数
     * @return 短信发送响应
     * @throws ClientException 异常
     */
    public static SendSmsResponse sendSms(String signName, String templateCode, String phone, Map<String, String> param) throws ClientException {

        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", PRODUCT, DOMAIN);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //组装请求对象-具体描述见控制台-文档部分内容
        SendSmsRequest request = new SendSmsRequest();
        //必填:待发送手机号
        request.setPhoneNumbers(phone);
        //必填:短信签名-可在短信控制台中找到
        request.setSignName(signName);
        //必填:短信模板-可在短信控制台中找到
        request.setTemplateCode(templateCode);
        request.setTemplateParam(JSON.toJSONString(param));

        return acsClient.getAcsResponse(request);
    }

    /**
     * 批量发送短信
     * 签名名称必须跟手机号长度一致
     *
     * @param signName     签名名称 ["阿里云","阿里巴巴"]
     * @param templateCode 模板code
     * @param phone        手机号 ["1590000****","1350000****"]
     * @param param        参数 [{"name":"TemplateParamJson"},{"name":"TemplateParamJson"}]
     * @return 发送结果
     * @throws ClientException
     */
    public static SendBatchSmsResponse batchSendSms(String signName, String templateCode, String phone, List<Map<String, String>> param) throws ClientException {

        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "20000");
        System.setProperty("sun.net.client.defaultReadTimeout", "20000");

        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", PRODUCT, DOMAIN);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //组装请求对象-具体描述见控制台-文档部分内容
        SendBatchSmsRequest request = new SendBatchSmsRequest();
        //必填:待发送手机号
        request.setPhoneNumberJson(phone);
        //必填:短信签名-可在短信控制台中找到
        request.setSignNameJson(signName);
        //必填:短信模板-可在短信控制台中找到
        request.setTemplateCode(templateCode);
        request.setTemplateParamJson(JSON.toJSONString(param));

        return acsClient.getAcsResponse(request);
    }

    /**
     * 查询短信发送结果
     *
     * @param phone 手机号
     * @param bizId 业务ID
     * @return 发送结果
     * @throws ClientException 异常
     */
    public static QuerySendDetailsResponse querySendDetails(String phone, String bizId) throws ClientException {
        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", PRODUCT, DOMAIN);
        IAcsClient acsClient = new DefaultAcsClient(profile);

        //组装请求对象
        QuerySendDetailsRequest request = new QuerySendDetailsRequest();
        //必填-号码
        request.setPhoneNumber(phone);
        //可选-流水号
        request.setBizId(bizId);
        //必填-发送日期 支持30天内记录查询，格式yyyyMMdd
        SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd");
        request.setSendDate(ft.format(new Date()));
        //必填-页大小
        request.setPageSize(10L);
        //必填-当前页码从1开始计数
        request.setCurrentPage(1L);

        //hint 此处可能会抛出异常，注意catch

        return acsClient.getAcsResponse(request);
    }

    public static void main(String[] args) throws ClientException, InterruptedException {
        List<String> codes = new ArrayList<>();
        codes.add("oWSYj8FT");
        codes.add("Wk96MWJb");
        codes.add("eL9iL3k6");
        codes.add("itLVrWjA");
        codes.add("aHtAKXiQ");

        Map<String, String> param = new HashMap<>(2);
        param.put("code", StringUtils.join(codes, "、"));

        List<String> codes1 = new ArrayList<>();
        codes1.add("C9yM2Dtq");
        codes1.add("Wk96MWJb");
        codes1.add("HyIjZpji");
        codes1.add("TiWJz2rA");
        codes1.add("ldsf8lxV");
        codes1.add("8byB04ox");
        codes1.add("2qSNGdE4");
        codes1.add("pNjqjLpM");
        codes1.add("oWSYj8FT");

        Map<String, String> param2 = new HashMap<>(2);
        param2.put("code", StringUtils.join(codes1, "、"));

        List<Map<String, String>> paramList = new ArrayList<>();
        paramList.add(param);
//        paramList.add(param2);


        String phone = "187******53";
        String phone1 = "180******09";
        List<String> phoneList = new ArrayList<>();
        phoneList.add(phone);
//        phoneList.add(phone1);

        String signName = "蚂蚁特工";
        List<String> signList = new ArrayList<>();
        signList.add(signName);
//        signList.add(signName);
        String templateCode = "SMS_115930706";
        //发短信
//        SendSmsResponse response = sendSms(signName, templateCode, phone, param);
        SendBatchSmsResponse response = batchSendSms(JSON.toJSONString(signList), templateCode, JSON.toJSONString(phoneList), paramList);
        System.out.println("短信接口返回的数据----------------");
        System.out.println("Code=" + response.getCode());
        System.out.println("Message=" + response.getMessage());
        System.out.println("RequestId=" + response.getRequestId());
        System.out.println("BizId=" + response.getBizId());

        Thread.sleep(3000L);

        //查明细
        if (response.getCode() != null && response.getCode().equals("OK")) {
            QuerySendDetailsResponse querySendDetailsResponse = querySendDetails(phone, response.getBizId());
            System.out.println("短信明细查询接口返回数据----------------");
            System.out.println("Code=" + querySendDetailsResponse.getCode());
            System.out.println("Message=" + querySendDetailsResponse.getMessage());
            int i = 0;
            for (QuerySendDetailsResponse.SmsSendDetailDTO smsSendDetailDTO : querySendDetailsResponse.getSmsSendDetailDTOs()) {
                System.out.println("SmsSendDetailDTO[" + i + "]:");
                System.out.println("Content=" + smsSendDetailDTO.getContent());
                System.out.println("ErrCode=" + smsSendDetailDTO.getErrCode());
                System.out.println("OutId=" + smsSendDetailDTO.getOutId());
                System.out.println("PhoneNum=" + smsSendDetailDTO.getPhoneNum());
                System.out.println("ReceiveDate=" + smsSendDetailDTO.getReceiveDate());
                System.out.println("SendDate=" + smsSendDetailDTO.getSendDate());
                System.out.println("SendStatus=" + smsSendDetailDTO.getSendStatus());
                System.out.println("Template=" + smsSendDetailDTO.getTemplateCode());
            }
            System.out.println("TotalCount=" + querySendDetailsResponse.getTotalCount());
            System.out.println("RequestId=" + querySendDetailsResponse.getRequestId());
        }


    }
}
